package com.maikang.exammall.entity;

import com.fasterxml.jackson.annotation.*;
import com.maikang.exammall.entity.constant.RepositoryConstant;
import com.maikang.exammall.util.validator.IdentityCardNumber;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.SuperBuilder;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
import org.hibernate.validator.constraints.Range;

import javax.persistence.*;
import javax.validation.constraints.*;
import java.util.Date;

import static com.maikang.exammall.entity.OrderItem.TABLE_NAME;

@Entity
@Table(name = TABLE_NAME, schema = RepositoryConstant.APP_DB_TABLE_SCHEMA, catalog = RepositoryConstant.APP_DB_TABLE_SCHEMA)
@DynamicUpdate
@DynamicInsert
@SuperBuilder
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true, value = {"hibernateLazyInitializer", "handler"})
@NoArgsConstructor
@AllArgsConstructor
@Data
@EqualsAndHashCode(callSuper=true, exclude = "order")
@ToString(callSuper = true, exclude = "order")
@SQLDelete(sql = "update " + RepositoryConstant.APP_DB_TABLE_SCHEMA + "." + TABLE_NAME + " set is_deleted = 1 WHERE id = ?")
@Where(clause = " is_deleted = 0")
public class OrderItem extends AuditEntity<Long> {
    public static final String TABLE_NAME = "mko_order_item";

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private Long refererId;
    private Long userId;
    @Column(name="order_id", updatable=false, insertable=false)
    private Long orderId;
    @ManyToOne(fetch = FetchType.LAZY)
    @JsonBackReference
    @ApiModelProperty(hidden = true)
    private Order order;
    @Positive
    private Long pkgId;
    @ApiModelProperty(hidden = true)
    private Long pkgPrice;

    @ApiModelProperty(hidden = true)
    @JsonIgnore
    private Double distributableRate;

    @ApiModelProperty(hidden = true)
    @JsonIgnore
    private Double firstSaleRate;

    @ApiModelProperty(hidden = true)
    @JsonIgnore
    private Double secondSaleRate;

    @ApiModelProperty(hidden = true)
    @JsonIgnore
    private Double manageRate;

    // 冗余 方便查询
    private String pkgName;
    // 冗余 方便查询
    private String pkgAvatarUrl;
    // 冗余 方便查询
    private String hospitalName;
    // 冗余 方便查询
    private Long hospitalId;
    // 冗余 方便查询
    private String hospitalAvatarUrl;

    @ApiModelProperty("支付状态: 0-待支付 2-付款成功，待预约 3-已发起退款 4-已完成退款 5-已过期/关闭 6 体检完毕，待评价 7 评价完毕")
    private Integer status;
    @ApiModelProperty("退款原因id, 3-买多了/不想买了 4-信息写错，重新下单 5-朋友/网上评价不好 6-计划有变，时间按排不上，7-其他")
    private Integer refundReasonId;
    @ApiModelProperty("退款原因的备注")
    private String refundReasonRemark;

    // examinee info
    @NotBlank
    private String examineeName;
    @Pattern(regexp = "1[3|4|5|7|8][0-9]\\d{8}")
    private String examineeMobile;
    @IdentityCardNumber
    @NotBlank
    private String idCardNo;
    @NotNull
    private Boolean isMarried;
    @Range(min = 1, max = 2)
    private Integer gender;
    // examinee info

    @JsonFormat(pattern = "yyyy-MM-dd")
    private Date examineDate;

    @Transient
    @JsonIgnore
    @ApiModelProperty(hidden = true)
    public Long getFirstLevelSaleCommission() {
        return new Double(pkgPrice.doubleValue() * firstSaleRate * distributableRate).longValue();
    }
    @Transient
    @JsonIgnore
    @ApiModelProperty(hidden = true)
    public Long getSecondLevelSaleCommission() {
        return new Double(pkgPrice.doubleValue() * secondSaleRate * distributableRate).longValue();
    }

    @JsonIgnore
    @ApiModelProperty(hidden = true)
    public Long getManageCommission() {
        return new Double(pkgPrice.doubleValue() * manageRate * distributableRate).longValue();
    }

    @Override
    @ApiModelProperty(hidden = true)
    @JsonIgnore
    public Long getPrimaryKey() {
        return this.id;
    }

    @Override
    public void setPrimaryKey(Long id) {
        this.id = id;
    }
}
